<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2023/5/29
 * Time: 17:50
 */
namespace app\admin\controller;

use app\middleware\AdminCheck;
use think\facade\Db;

class Notice{

    protected $middleware = [
        AdminCheck::class => ['except' 	=> [''] ]
    ];



    /**
     * 添加
     * @return \think\response\Json
     */
    public function add(){
        validateParam('content','content');

        $data['content'] = input('content');
        $data['update_at'] = currentTime();
        $data['create_at'] = currentTime();
        Db::name('notice')->insert($data);
        return success('添加成功');
    }




    /**
     * 列表
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getList(){
        validateParam('pageNo,pageSize','pageNo,pageSize');
        $param=input();
        $where[]=['is_deleted','=',0];


        //获取总数
        $total=Db::name('notice')
            ->where($where)
            ->count();

        $offset=makeOffset($param['pageNo'],$param['pageSize']);

        $list=Db::name('notice')
            ->where($where)
            ->limit($offset,$param['pageSize'])
            ->order('create_at desc')
            ->select();
        return success('列表',['total'=>$total,'list'=>$list]);
    }



    /**
     * 编辑
     */
    public function edit(){
        validateParam('id,content','id,content');

        $data['id'] = input('id');
        $data['content'] = input('content');
        $data['update_at'] = currentTime();
        Db::name('notice')->save($data);
        return success('编辑成功');
    }




    /**
     * 删除
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     */
    public function delete(){
        validateParam('ids','ids');

        Db::name('notice')
            ->where('id','in',input('ids'))
            ->delete();
        return success('删除成功');
    }
}